require('./order.css');
import {
    setDeliver,
    addOrder
} from './ajax';


$(function () {

    $('.check').iCheck({
        radioClass: 'iradio_square-blue'
    });



    //设置配送方式
    $('.check').on('ifChecked', function () {

        let province = $('.province').text();
        let shipid = $(this).val();
        setDeliver(province, shipid, (res) => {
            if (res.rel != 1) {
                $.toast(res.msg);
            }
            $('.shipfee').text(res.shipfee);
            $('.total-price').text(res.sum);
        });

    });


    //提交订单
    $('#addOrder').click(function () {
        let shipid = $('input[name="deliver"]:checked').val();
        if (!shipid) {
            $.toast('请选择一种配送方式');
            return false;
        }
        let gids = $(this).data('gids');
        let addid = $(this).data('addid');

        addOrder(shipid, gids, addid, (res) => {
            if (res.rel == 1) {
                let markup = `
                    <div>您的订单号：<span>${res.data.ordersn}</span></div>
                    <div>您的配送方式：<span>${res.data.shipname}</span></div>
                    <div>您的应付款金额：<span>${res.data.total}</span>元</div>
                `;
                $.modal({
                    title: res.msg,
                    text: markup,
                    buttons: [{
                            text: '首页',
                            onClick: function () {
                                location.href = _indexUrl;
                            }
                        },
                        {
                            text: '个人中心',
                            bold: true,
                            onClick: function () {
                                location.href = _memberUrl;
                            }
                        },
                    ]
                });

            } else {
                $.toast(res.msg);
            }
        });

    });
});